#include <stdlib.h>
#include "vec3.h"
#include "PARTICLE.h"
#include "GENERATOR.h"
#include <time.h>

void Generator::Init(Vec3 _center, float _radius, float _delay)
{
	qtdPart = GEN_MAX_PART;
	qtdAlive = 0;
	center = _center;
	radius = _radius;
	delay = _delay;

	srand( (unsigned int)time(NULL) );

	for( int i = 0; i < qtdPart; i++ )
	{
		Vec3 pos;
		do
		{
			float x, y, z;
			x = ((float)rand()/RAND_MAX) * (2*radius) - radius;
			y = /*( ( rand()%(int)(2*radius + 0.5f) )-radius )*/0;
			z = ((float)rand()/RAND_MAX) * (2*radius) - radius;
			pos.Set(x, y, z);
		}while( pos.Length() >= radius );

		pos += center;
		vetPart[i].Init( pos.x, pos.y, pos.z, 1, 1.0f );
	}
}

void Generator::Draw()
{
	for( int i=0; i<qtdPart; ++i )
	{
		vetPart[i].Draw();
	}
}

void Generator::Update(float dt)
{
	static float countDown = delay;
	
	if( qtdAlive < qtdPart )
	{
		countDown -= dt;

		if( countDown <= 0 )
		{
			Drop();
			countDown = delay;
		}
	}
}

void Generator::Drop()
{
	vetPart[qtdAlive].alive = true;
	qtdAlive++;
}